﻿Imports Data

Public Class Banco

    Private Property id_banco As Integer

    Private Sub Banco_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.CargarLV()
    End Sub

    Private Function CargarLV() As Boolean
        Try
            Me.lvBanco.Items.Clear()
            Using db As New SerigraphicaDBDataContext
                Dim lista As List(Of Data.Banco) = db.Banco.Select(Function(p) p).ToList
                For Each item In lista
                    Me.lvBanco.Items.Add(New ListViewItem(New String() {item.nombre, _
                                                                         item.propietario, _
                                                                         item.numero_cuenta, _
                                                                         item.nombre_ejecutivo, _
                                                                         item.telefono_ejecutivo, _
                                                                         item.email}) With {.Name = item.id})
                Next
            End Using
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Private Sub IngresarToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles IngresarToolStripMenuItem.Click
        Me.LimpiarInterfaz()
    End Sub

    Private Function LimpiarInterfaz() As Boolean
        Try
            Me.txtNombre.Text = String.Empty
            Me.txtEmailEjecutivo.Text = String.Empty
            Me.txtNombreEjecutivo.Text = String.Empty
            Me.txtNumeroCuenta.Text = String.Empty
            Me.txtPropietario.Text = String.Empty
            Me.txtTelefonoEjecutivo.Text = String.Empty
            Me.btnIngresar.Text = "Ingresar"
            Me.txtNombre.Focus()
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Private Sub btnIngresar_Click(sender As System.Object, e As System.EventArgs) Handles btnIngresar.Click
        Try
            If Me.btnIngresar.Text = "Ingresar" Then
                Dim n_banco As New Data.Banco With {.nombre = txtNombre.Text, _
                                                   .propietario = txtPropietario.Text, _
                                                   .numero_cuenta = txtNumeroCuenta.Text, _
                                                   .nombre_ejecutivo = txtNombreEjecutivo.Text, _
                                                   .telefono_ejecutivo = txtTelefonoEjecutivo.Text, _
                                                   .email = txtEmailEjecutivo.Text}

                If Me.IngresarBanco(n_banco) Then
                    MsgBox("Ingresado Correctamente", MsgBoxStyle.OkCancel)
                    Me.LimpiarInterfaz()
                    Me.CargarLV()
                End If
            Else
                Me.id_banco = Me.lvBanco.SelectedItems(0).Name
                If Me.ModificarBanco Then
                    MsgBox("Modificado Correctamente", MsgBoxStyle.OkOnly)
                    Me.LimpiarInterfaz()
                    Me.CargarLV()
                End If
            End If

        Catch ex As Exception

        End Try
    End Sub

    Private Function IngresarBanco(ByVal n_banco As Data.Banco) As Boolean
        Try
            Using db As New SerigraphicaDBDataContext
                db.Banco.InsertOnSubmit(n_banco)
                db.SubmitChanges()
            End Using
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Private Function ModificarBanco()
        Try
            Using db As New SerigraphicaDBDataContext
                Dim banco As Data.Banco = db.Banco.Where(Function(p) p.id = Me.id_banco).Select(Function(p) p).FirstOrDefault
                banco.nombre = txtNombre.Text
                banco.nombre_ejecutivo = txtNombreEjecutivo.Text
                banco.numero_cuenta = txtNumeroCuenta.Text
                banco.propietario = txtPropietario.Text
                banco.telefono_ejecutivo = txtTelefonoEjecutivo.Text
                db.SubmitChanges()
            End Using
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Private Sub ModificarToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ModificarToolStripMenuItem.Click
        Try
            Me.ModificarInterfaz()
        Catch ex As Exception

        End Try
    End Sub

    Private Function ModificarInterfaz() As Boolean
        Try
            Me.id_banco = Me.lvBanco.SelectedItems(0).Name
            Using db As New SerigraphicaDBDataContext
                Dim m_banco As Data.Banco = db.Banco.Where(Function(p) p.id = id_banco).Select(Function(p) p).FirstOrDefault
                Me.txtNombre.Text = m_banco.nombre
                Me.txtNombreEjecutivo.Text = m_banco.nombre_ejecutivo
                Me.txtNumeroCuenta.Text = m_banco.numero_cuenta
                Me.txtPropietario.Text = m_banco.propietario
                Me.txtTelefonoEjecutivo.Text = m_banco.telefono_ejecutivo
                Me.txtEmailEjecutivo.Text = m_banco.email
                Me.btnIngresar.Text = "Modificar"
            End Using
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Private Sub EliminarToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles EliminarToolStripMenuItem.Click
        Try
            If Me.EliminarBanco() Then
                MsgBox("Eliminacion Exitosa", MsgBoxStyle.OkOnly)
                Me.LimpiarInterfaz()
                Me.CargarLV()
            Else
                MsgBox("Error")
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Function EliminarBanco() As Boolean
        Try
            Using db As New SerigraphicaDBDataContext
                Me.id_banco = Me.lvBanco.SelectedItems(0).Name
                Dim banco As Data.Banco = db.Banco.Where(Function(p) p.id = id_banco).Select(Function(p) p).FirstOrDefault
                db.Banco.DeleteOnSubmit(banco)
                db.SubmitChanges()
            End Using
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function
End Class